package gold.digger;

import gold.utils.InputUtil;

import java.util.Arrays;

/**
 * Created by fanzhenyu02 on 2021/12/10.
 * common problem solver template.
 */
public class LC1040 {
    public long startExecuteTime = System.currentTimeMillis();


    /*
     * @param 此题目参考了别人代码
     * 这是因为问题情况较为复杂
     * 未来需要再次复习此道题目
     * 这题目有点目瞪狗呆
     * 参考精选答案： https://leetcode-cn.com/problems/moving-stones-until-consecutive-ii/solution/jie-ti-si-lu-by-owenzzz/
     * @return:
     */
    class Solution {
        public int[] numMovesStonesII(int[] stones) {
            Arrays.sort(stones);
            int n = stones.length;
            int maxEmpty = stones[n - 1] - stones[0] + 1 - n;
            maxEmpty -= Math.min(stones[n - 1] - stones[n - 2] - 1, stones[1] - stones[0] - 1);
            int minVal = maxEmpty, i = 0, j = 0;
            for (i = 0; i < n; ++i) {
                while (j + 1 < n && stones[j + 1] - stones[i] + 1 <= n) ++j;
                int cost = n - (j - i + 1);
                if (j - i + 1 == n - 1 && stones[j] - stones[i] + 1 == n - 1) cost = 2;
                minVal = Math.min(minVal, cost);
            }
            return new int[]{minVal, maxEmpty};
        }
    }

    public void run() {
        int[] arr = InputUtil.toIntegerArray("[1,2,3]");
        System.out.println(new Solution().toString());
    }

    public static void main(String[] args) throws Exception {
        LC1040 an = new LC1040();
        an.run();

        System.out.println("\ncurrent solution total execute time: " + (System.currentTimeMillis() - an.startExecuteTime) + " ms.");
    }
}
